#version 460 core

#define M_PI 3.1415926535897932384626433832795

layout (location = 0) out vec4 kernel_value;

layout (rgba32f, binding = 0) uniform image2D world_pos_map;

uniform int kernel_w, kernel_h;
uniform ivec2 index_kernel_iv;
uniform float s;

in vec3 FragPos;
in vec2 TexCoord;

float fDiffuseProfile(float r, float A, float s) // A = 0.1, s = 3.78
{
	return A * s * ( ( exp(-s * r) + exp(-s * r / 3) ) / (8 * M_PI * r) );
}

void main()
{             
	ivec2 coord = ivec2(gl_FragCoord.xy);
	vec3 pos_tex = imageLoad(world_pos_map, index_kernel_iv).xyz;
	if (pos_tex == vec3(0, 0, 0))
	{
		if (coord == ivec2(0, 0))
			kernel_value = vec4(0, 0, 1, 0);
		else
			kernel_value = vec4(0, 0, 0, 0);
	}
	else
	{
		vec3 pos_cur = imageLoad(world_pos_map, coord).xyz;
		float r = length(pos_tex - pos_cur);
		if (r == 0)
			kernel_value = vec4(0, 0, 0, 0);
		else if (r < 2)
		{
			float fs = fDiffuseProfile(r, 1.0, s);
			kernel_value = vec4(fs, 0, 0, 0);
		}
	}
}